﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents a cell in a table.
    /// </summary>
    /// <remarks>TBody is implemented so as to implement table base
    /// attributes.</remarks>
    [Serializable]
    public class Td : TBody {

        /// <summary>
        /// Represents the possible scope choices for a cell.
        /// </summary>
        public enum ScopeChoices {
            /// <summary>
            /// The cell is a header for a column.
            /// </summary>
            Col,
            /// <summary>
            /// The cell is a header for a row.
            /// </summary>
            Row,
            /// <summary>
            /// The cell is a header for a column group.
            /// </summary>
            ColGroup,
            /// <summary>
            /// The cell is a header for a row group.
            /// </summary>
            RowGroup
        }

        private string _abbr;
        private ICollection<Element> _axis;
        private int? _colspan;
        private ICollection<Element> _headers;
        private int? _rowspan;
        private ScopeChoices? _scope;

        /// <summary>
        /// Constructs a new Td element.
        /// </summary>
        public Td() {
            _axis = new LinkedList<Element>();
            _headers = new LinkedList<Element>();
        }

        /// <summary>
        /// Represents a summary of the contents of the cell.
        /// </summary>
        [Obsolete("Deprecated in HTML5 - consider starting the cell with an <abbr>.")]
        public string Abbr {
            get { return _abbr; }
            set { _abbr = value; }
        }

        /// <summary>
        /// Determines the cells that this cell is a header of.
        /// </summary>
        [Obsolete("Deprecated in HTML5 - use \"scope\" instead.")]
        public ICollection<Element> Axis {
            get { return _axis; }
        }

        /// <summary>
        /// Determines how many columns after this one to span this cell.
        /// </summary>
        public int? ColSpan {
            get { return _colspan; }
            set {
                if (value == null || value > 0) {
                    _colspan = value;
                } else {
                    throw new ArgumentException(
                        "ColSpan must be greater than zero.", "value");
                }
            }
        }

        /// <summary>
        /// Determines the headers of this cell.
        /// </summary>
        public ICollection<Element> Headers {
            get { return _headers; }
        }

        /// <summary>
        /// Determines the number of rows after this one to span this cell.
        /// </summary>
        public int? RowSpan {
            get { return _rowspan; }
            set { _rowspan = value; }
        }

        /// <summary>
        /// Determines what group of cells this header cell applies to.
        /// </summary>
        public ScopeChoices? Scope {
            get { return _scope; }
            set { _scope = value; }
        }

        /// <summary>
        /// Gets a formatted string of all local attributes of this element
        /// (not including the base attributes).
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's
        /// attribute data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _abbr, "abbr");

            string axis = "";
            if (_axis.Count != 0) {
                
                foreach (Element e in _axis) {
                    axis += e.ID + ' ';
                }
                AttributeWriter.Write(sb, axis, "axis");
            }

            AttributeWriter.Write(sb, _colspan, "colspan");

            if (_headers.Count != 0) {
                axis = "";
                foreach (Element e in _headers) {
                    axis += e.ID + ' ';
                }
                AttributeWriter.Write(sb, axis, "headers");
            }

            AttributeWriter.Write(sb, _rowspan, "rowspan");
            AttributeWriter.Write(sb, _scope, "scope", true);
            base.WriteLocalAttributes(sb);
        }
    }
}
